<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/12/21
 * Time: 下午5:09
 */
class Msg_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
    }

    //查询卡
    public function serch_card($item_id)
    {
        $where = array('status' => 0, 'item_id' => $item_id);
        $info  = $this->db->where($where)->order_by('id', 'asc')->limit(1)->from('app_card')->get()->row_array();
        if ($info) {
            return $info;
        } else {
            return false;
        }
    }

    //修改卡的状态
    public function modify_card($name, $phone, $card, $order_bn)
    {
        $where               = array('card' => $card);
        $dataArr             = array();
        $dataArr['name']     = $name;
        $dataArr['phone']    = $phone;
        $dataArr['order_bn'] = $order_bn;
        $dataArr['status']   = 1;
        $res                 = $this->db->where($where)->update('app_card', $dataArr);
        if ($res !== fasle) {
            return true;
        } else {
            return false;
        }

    }

    //添加卡
    public function add_card($client, $item_id)
    {
        $method      = 'youzan.items.onsale.get'; //要调用的api名称
        $api_version = '3.0.0'; //要调用的api版本号

        $my_params = [
            'page_no'   => '1',
            'page_size' => '10',
        ];

        $my_files = [
        ];

        $info = $client->post($method, $api_version, $my_params, $my_files);

        $where  = array('status' => 0, 'item_id' => $item_id);
        $number = $this->db->where($where)->from('app_card')->count_all_results();

        if ($number < 20) {
            foreach ($info['response']['items'] as $key => $value) {
                if ($value['item_id'] == $item_id) {
                    $title = $value['title'];
                }
            }

            for ($i = 0; $i < 100; $i++) {
                list($s1, $s2) = explode(' ', microtime());
                $card          = (float) sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
                $card          = randomLetters(2) . mb_substr($card, -8);
                $pwd           = random(6, '1234567890');
                usleep(1000);
                $dataArr                = array();
                $dataArr['item_id']     = $item_id;
                $dataArr['title']       = $title;
                $dataArr['card']        = $card;
                $dataArr['password']    = $pwd;
                $dataArr['create_time'] = date("Y-m-d H:i:s", time());
                $res                    = $this->db->insert('app_card', $dataArr);
            }
            file_get_contents("http://119.23.141.210:10337/server/msg/makeExcel");
        }
    }

    //查询卡
    public function find_card($where = array())
    {
        return $this->db->from('app_card')->order_by('id', 'desc')->limit(100)->get()->result_array();
    }

    //获取有赞订单信息
    public function get_youzan_order_info($client, $id)
    {
        $method      = 'youzan.trade.get'; //要调用的api名称
        $api_version = '4.0.0'; //要调用的api版本号
        $my_params   = [
            'tid' => $id,
        ];
        $my_files = [
        ];
        return $client->post($method, $api_version, $my_params, $my_files);
    }

    //有赞自动确认收货
    public function auto_confirm($client, $order_bn)
    {
        $method      = 'youzan.logistics.online.confirm'; //要调用的api名称
        $api_version = '3.0.0'; //要调用的api版本号
        $my_params   = [
            'tid'           => $order_bn,
            'is_no_express' => '1',
        ];
        $my_files = [
        ];
        $client->post($method, $api_version, $my_params, $my_files);
    }

}
